<?php

namespace App\Controller\Admin;

use App\Controller\Example\BarsGoodsExample;
use App\Models\AdminModel;
use App\Models\BarsGoodsModel;
use App\Models\Conn;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;

class BarsGoods
{
    /**
     * 管理后台商品列表
     * entry token,name,type,status,startTime,endTime,page,limit
     * return json
     * */
    public function barsGoodsList($data){
        $assemble = Common::assemble(new BarsGoodsExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE `status` >= 0 ";
        if($assemble->name !=null){ $joinSql.=" and `name` like '%$assemble->name%' ";}
        if($assemble->type !=null){ $joinSql.=" and `type` ='$assemble->type' ";}
        if($assemble->status !=null){ $joinSql.=" and `status` ='$assemble->status' ";}
        if($assemble->startTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $sql = "SELECT `id`,`bid`,`cid`,`name`,`synopsis`,`price`,`integral`,`original_price`,`cost_price`,`sales`,`reality_sales`,`img`,`video`,`details`,`time`,`stock`,`status`,`type`,`discount`,`rebate`,`views`,`sort`,`sku`,`times` FROM `bars_goods` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($sql,true);
        foreach ($readConn as $item=>$value){
            $goodsImgsql = "SELECT `img` FROM `goods_img` where `gid` = $value[id]";
            $readConn[$item]["slideshow"] = $conn->readConn($goodsImgsql,true);
        }
        $returnData["list"]=$readConn;
        $countSql="SELECT count(id) as count FROM `bars_goods` $joinSql";
        $returnData["count"] = $conn->readConn($countSql)["count"];
        $returnData["total"] = null;
        Result::success($returnData);
    }
    /**
     * 管理后台添加商品
     * entry token,bid,cid,name,synopsis,price,integral,original_price,cost_price,sales,reality_sales,img,video,details,time,stock,status,type,discount,rebate,views,sort,sku
     * return json
     * */
    public function addBarsGoods($data){
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $data["token"]);
        if($adminInToken["level"] !=1){
            if($data["status"] ==1){
                //Result::error(ErrorEnum::ONLYSUPERADMINISTRATORSCANBELISTED);
            }
        }
        if(!empty($data["id"])){
            self::editBarsGoods($data);
        }
        $goodsImgs = explode(",", $data["slideshow"]);
        $assemble = Common::assemble(new BarsGoodsExample(), $data);

        $conn->shiwuStart();
        $assemble->sku = json_encode($assemble->sku,JSON_UNESCAPED_UNICODE);

        if(BarsGoodsModel::insertBarsGoods($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $mysql_insert_id = $conn->mysql_insert_id();
        self::insetGoodsImg($conn,$goodsImgs,$mysql_insert_id);
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"商品管理","添加商品:[".$assemble->name."]");
        Result::success("ok");
    }
    /**
     * 管理后台编辑商品
     * entry token,id,bid,cid,name,synopsis,price,integral,original_price,cost_price,sales,reality_sales,img,video,details,time,stock,status,type,discount,rebate,views,sort,sku
     * return json
     * */
    public function editBarsGoods($data){
        $assemble = Common::assemble(new BarsGoodsExample(), $data);
        $goodsImgs = explode(",", $data["slideshow"]);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $conn->shiwuStart();
        $assemble->sku = json_encode($assemble->sku,JSON_UNESCAPED_UNICODE);
        if(BarsGoodsModel::upBarsGoodsInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        self::delGoodsImg($conn,$assemble->id);
        self::insetGoodsImg($conn,$goodsImgs,$assemble->id);
        $conn->shiwuEnd();

        AdminLog::addAdminLog($adminInToken["username"],"商品管理","编辑商品:[".$assemble->name."]");
        Result::success("ok");
    }
    /**
     * 管理后台删除商品
     * entry
     * return json
     * */
    public function delBarsGoods($data){
        $assemble = Common::assemble(new BarsGoodsExample(), $data);
        if(empty($assemble->id)){
            Result::error(ErrorEnum::ITEMDOESNOTEXIST);
        }
        $assemble->status = -1;
        $conn = new Conn();
        $conn->shiwuStart();
        if(BarsGoodsModel::upBarsGoodsInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"商品管理","删除商品:[".$assemble->id."]");
        Result::success("ok");
    }
    /**
     * 管理后台商品轮播图处理
     * entry
     * return json
     * */
    public static function insetGoodsImg($conn,$goodsImgs,$gid){
        foreach ($goodsImgs as $key=>$val){
            $insetGoodsImgSql="INSERT INTO `goods_img`(`gid`, `img`) VALUES ($gid,'$val')";
            if($conn->executeConn($insetGoodsImgSql) !=1){
                $conn->shiwuRollback();
                Result::error(ErrorEnum::SUBMITFAILED);
            }
        }
    }
    /**
     * 管理后台商品轮播图删除
     * entry
     * return json
     * */
    public static function delGoodsImg($conn,$goodsImgId){
        $insetGoodsImgSql="DELETE FROM `goods_img` WHERE `gid`= $goodsImgId";
        $conn->executeConn($insetGoodsImgSql);
    }
}